package com.zhy.baselibrary.utils.dialog

import android.content.Context
import android.graphics.Color
import android.text.Html
import android.view.Gravity
import android.view.View
import android.widget.ImageView
import android.widget.RelativeLayout
import android.widget.TextView
import com.blankj.utilcode.util.ScreenUtils
import com.lxj.xpopup.core.CenterPopupView
import com.lxj.xpopup.interfaces.OnCancelListener
import com.lxj.xpopup.interfaces.OnConfirmListener
import com.zhy.baselibrary.R
import com.zhy.baselibrary.utils.clickWithTrigger


class DialogView(context: Context, title: String, message: String, isShowTitle: Boolean) : CenterPopupView(context) {
    private var title = title
    private var message = message
    private var cancelColor = "#666666"
    private var confirmColor = "#222222"
    private var msgColor = "#222222"
    private var cancelListener: OnCancelListener? = null
    private var confirmListener: OnConfirmListener? = null
    private var jumpListener: OnJumpListener? = null
    private var jump = ""
    private var cancel = "取消"
    private var confirm = "确定"
    private var isShowTitle = isShowTitle
    private var gravity: Int = Gravity.CENTER
    private var isHintCancel = false // true 隐藏 false 显示
    private var isHintJump = true // true 隐藏 false 显示
    private var isHintClose = true // true 隐藏 false 显示
    private var isShowTitleView = true // true 隐藏 false 显示

    override fun getImplLayoutId(): Int = R.layout.dialog_view
    override fun onCreate() {
        super.onCreate()
        var rlTitle = findViewById<RelativeLayout>(R.id.rlTitle)
        var title_view = findViewById<View>(R.id.title_view)
        var tvJump = findViewById<TextView>(R.id.tvJump)
        var tvTitle = findViewById<TextView>(R.id.tvTitle)
        var tvMessage = findViewById<TextView>(R.id.tvMessage)
        var vLine = findViewById<View>(R.id.vLine)
        var tvCancel = findViewById<TextView>(R.id.tvCancel)
        var tvConfirm = findViewById<TextView>(R.id.tvConfirm)
        var ivClose = findViewById<ImageView>(R.id.ivClose)
        if (isShowTitle) {
            rlTitle.visibility = View.VISIBLE
        } else {
            rlTitle.visibility = View.GONE
        }

        if (isShowTitleView) {
            title_view.visibility = View.VISIBLE
        } else {
            title_view.visibility = View.GONE
        }
        tvJump.text = jump
        if (title.isNullOrEmpty()) {
            tvTitle.visibility = View.GONE
        } else {
            tvTitle.visibility = View.VISIBLE
            tvTitle.text = Html.fromHtml(title)
        }
        if (message.isNullOrEmpty()) {
            tvMessage.visibility = View.GONE
        } else {
            tvMessage.gravity = gravity
            tvMessage.visibility = View.VISIBLE
            tvMessage.text = Html.fromHtml(message)
        }
        tvMessage.setTextColor(Color.parseColor(msgColor))
        tvCancel.text = cancel
        tvConfirm.text = confirm
        if (!isHintCancel) {
            tvCancel.visibility = View.VISIBLE
            vLine.visibility = View.VISIBLE
        } else {
            tvCancel.visibility = View.GONE
            vLine.visibility = View.GONE
        }
        if (!isHintJump) {
            tvJump.visibility = View.VISIBLE
        } else {
            tvJump.visibility = View.GONE
        }
        if (isHintClose) {
            ivClose.visibility = View.INVISIBLE
        } else {
            ivClose.visibility = View.VISIBLE
        }
        if (cancelColor.isNullOrEmpty()) {
            tvCancel.setTextColor(Color.parseColor("#666666"))
        } else {
            tvCancel.setTextColor(Color.parseColor(cancelColor))
        }
        if (confirmColor.isNullOrEmpty()) {
            tvConfirm.setTextColor(Color.parseColor("#222222"))
        } else {
            tvConfirm.setTextColor(Color.parseColor(confirmColor))
        }
        if (confirm != "") {
            tvConfirm.text = confirm
        }
        if (cancel != "") {
            tvCancel.text = cancel
        }
        tvCancel.clickWithTrigger(500) {
            dismiss()
            cancelListener?.onCancel()
        }
        tvConfirm.clickWithTrigger(500) {
            dismiss()
            confirmListener?.onConfirm()
        }
        ivClose.clickWithTrigger(500) {
            dismiss()
        }
    }

    fun isHintCancel(isHintCancel: Boolean) {
        this.isHintCancel = isHintCancel
    }

    fun isShowTitleView(isShowTitleView: Boolean) {
        this.isShowTitleView = isShowTitleView
    }

    fun cancelColor(color: String) {
        this.cancelColor = color
    }

    fun messageGravity(gravity: Int) {
        this.gravity = gravity
    }

    fun cancel(cancel: String, cancelListener: OnCancelListener?) {
        this.cancel = cancel
        this.cancelListener = cancelListener
    }

    fun cancel(cancel: String, color: String, cancelListener: OnCancelListener?) {
        this.cancel = cancel
        this.cancelColor = color
        this.cancelListener = cancelListener
    }

    fun confirmColor(color: String) {
        this.confirmColor = color
    }

    fun msgColor(color: String) {
        this.msgColor = color
    }

    fun confirm(confirm: String, color: String, confirmListener: OnConfirmListener?) {
        this.confirm = confirm
        this.confirmColor = color
        this.confirmListener = confirmListener
    }

    fun confirm(confirm: String, confirmListener: OnConfirmListener?) {
        this.confirm = confirm
        this.confirmListener = confirmListener
    }

    fun jump(jump: String, jumpListener: OnJumpListener?) {
        this.jump = jump
        this.isHintJump = jump.isNullOrEmpty()
        this.jumpListener = jumpListener
    }

    fun close(isHintClose: Boolean = true) {
        this.isHintClose = isHintClose
    }

    override fun getMaxWidth(): Int {
        return ScreenUtils.getScreenWidth()
    }


    interface OnJumpListener {
        fun jump()
    }
}